<script setup lang="ts">
import { onUnmounted, ref } from 'vue'
import { mobileRules, passwordRules, codeRules } from '@/utils/rules'
import { showSuccessToast, showToast } from 'vant'
import { loginAPI, sendCodeAPI, loginByCodeAPI } from '@/services/user'
import { useUserStore } from '@/stores/user'
import { useRoute, useRouter } from 'vue-router'
let timeId: number

const mobile = ref('13230000001')
const password = ref('abc12345')
const store = useUserStore()

const agree = ref(false)
const show = ref(false)
const router = useRouter()
const route = useRoute()
console.log(route.query)

const onLogin = async () => {
  if (!agree.value) {
    showToast('请勾选我已同意')
    return
  }
  const res = isPass.value
    ? await loginAPI(mobile.value, password.value)
    : await loginByCodeAPI(mobile.value, code.value)
  // localStorage.setItem('doctor-h5', JSON.stringify(res.data))
  store.saveUser(res.data)
  showSuccessToast('登录成功')
  const returnUrl = route.query.returnUrl
  if (returnUrl) {
    router.push(returnUrl as string)
  } else {
    router.push('/')
  }
}

const isPass = ref(true)
const code = ref('')
const second = ref(0)

const onSendCode = async () => {
  const res = await sendCodeAPI(mobile.value)
  console.log(res)

  second.value = 60
  timeId = setInterval(() => {
    second.value--
    if (second.value === 0) {
      clearInterval(timeId)
    }
  }, 1000)
}

onUnmounted(() => {
  clearInterval(timeId)
})
</script>

<template>
  <div class="login-page">
    <cp-nav-bar right-text="注册"></cp-nav-bar>
    <div class="login-head">
      <h3>{{ isPass ? '密码登录' : '短信验证码登录' }}</h3>
      <a href="javascript:;">
        <span @click="isPass = !isPass">{{ !isPass ? '密码登录' : '短信验证码登录' }}</span>
        <van-icon name="arrow"></van-icon>
      </a>
    </div>
    <!-- form 表单 -->
    <van-form @submit="onLogin" autocomplete="off">
      <van-field
        :rules="mobileRules"
        v-model="mobile"
        placeholder="请输入手机号"
        type="tel"
      ></van-field>
      <van-field
        v-if="isPass"
        :rules="passwordRules"
        v-model="password"
        placeholder="请输入密码"
        :type="show ? 'text' : 'password'"
      >
        <template #button>
          <cp-icon :name="`login-eye-${show ? 'on' : 'off'}`" @click="show = !show"></cp-icon>
        </template>
      </van-field>

      <van-field
        v-if="!isPass"
        :rules="codeRules"
        v-model="code"
        placeholder="短信验证码"
        :type="!isPass ? 'text' : 'password'"
      >
        <template #button>
          <span class="btn-send" v-show="second === 0" @click="onSendCode">发送验证码</span>
          <span class="btn-send" v-show="second > 0">{{ second }}s后再次发送</span>
        </template>
      </van-field>

      <div class="cp-cell">
        <van-checkbox v-model="agree">
          <span>我已同意</span>
          <a href="javascript:;">用户协议</a>
          <span>及</span>
          <a href="javascript:;">隐私条款</a>
        </van-checkbox>
      </div>
      <div class="cp-cell">
        <van-button block round type="primary" native-type="submit">登 录</van-button>
      </div>
      <div class="cp-cell">
        <a href="javascript:;">忘记密码？</a>
      </div>
    </van-form>

    <div class="login-other">
      <van-divider>第三方登录</van-divider>
      <div class="icon">
        <img src="@/assets/qq.svg" alt="" />
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-head {
    padding: 30px 30px 50px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    line-height: 1;
    h3 {
      font-weight: normal;
      font-size: 24px;
    }
    a {
      font-size: 15px;
    }
  }
  &-other {
    margin-top: 60px;
    padding: 0 30px;
    .icon {
      display: flex;
      justify-content: center;
      img {
        width: 36px;
        height: 36px;
        padding: 4px;
      }
    }
  }
}

.van-form {
  .btn-send {
    color: var(--cp-primary);
  }
  padding: 0 14px;
  .cp-cell {
    height: 52px;
    line-height: 24px;
    padding: 14px 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    .van-checkbox {
      a {
        color: var(--cp-primary);
        padding: 0 5px;
      }
    }
  }
}
</style>

function loginApI(value: string, value1: string) { throw new Error('Function not implemented.') }
function loginApI(value: string, value1: string) { throw new Error('Function not implemented.') }
